package Concurrency2.Chapter01_Base;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Instruction：工具类的使用及弊端分析
 * Author：@author MaLi
 */
public class T09_Executors {
    public static void main(String[] args) {
        //创建只有线程数固定大小的线程池
        ExecutorService pool = Executors.newFixedThreadPool(1);
        /**
         * 提交Runnable
         * 1, 包Runnable包装成为Calllable
         * FutureTask被当做RunnableFuture执行的时候, 会被设置返回值
         *
         */
        //提交Runnable, 并且有返回值
        /**
         * 提交Callable的执行原理分析
         * 1, 提交的是一个callable
         * 2, callable会被包装成为FutureTask(Runnable,Future)
         * 所以, 提交的是FutureTask, 返回过来的也是FutureTask, 并且在返回之前, 已经执行这个FutureTask
         */
        Future<String> future = pool.submit(() -> {
            System.out.println("---> result");
            return "hello java.utils.concurrent";
        });

        try {
            String result = future.get();
            System.out.println(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        pool.shutdown();
    }
}
